﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Narwhunderful.Aphelia.Framework.Graphics;

namespace Narwhunderful.Aphelia.Framework.UI
{

    public class TransitionEffect : Camera2D
    {

        float _count = 0.0f;

        float _progress = 0.0f;
        protected internal float Progress
        {
            get
            {
                return _progress;
            }
            set
            {
                _progress = value;
            }
        }

        float _transitionOffset = 0.0f;
        protected internal float TransitionOffset
        {
            get
            {
                return _transitionOffset;
            }
            set
            {
                _transitionOffset = value;
            }
        }

        float _invertedTransitionOffset = 0.0f;
        protected internal float InvertedTransitionOffset
        {
            get
            {
                return _invertedTransitionOffset;
            }
            set
            {
                _invertedTransitionOffset = value;
            }
        }

        int _interval = 0;
        public int Interval
        {
            get
            {
                return _interval;
            }
            set
            {
                _interval = value;
            }
        }

        UIScreen _currentScreen;
        public UIScreen CurrentScreen
        {
            get
            {
                return _currentScreen;
            }
            internal set
            {
                _currentScreen = value;
            }
        }

        protected internal bool IsTransitioningOn
        {
            get
            {
                if (_currentScreen == null) return false;
                return (_currentScreen.TransitionState == TransitionState.TransitioningOn);
            }
        }

        protected internal bool IsTransitioningOff
        {
            get
            {
                if (_currentScreen == null) return false;
                return (_currentScreen.TransitionState == TransitionState.TransitioningOff);
            }
        }

        public TransitionEffect(Game game)
            : base(game)
        {
            base.RasterizerState.ScissorTestEnable = true;
            Reset();
        }

        #region IUpdatableComponent

        protected override void InternalUpdate(GameTime gameTime)
        {
            if (_interval <= 0)
            {
                _progress = 1;
                _count = _interval;
            }
            else
            {
                _count += gameTime.ElapsedGameTime.Milliseconds;
                if (_count >= _interval) _count = _interval;
                _progress = _count / _interval;
            }
            MathHelper.Clamp(_progress, 0.0f, 1.0f);
            this.UpdateTransitionOffsets();
            base.InternalUpdate(gameTime);
        }

        protected virtual void UpdateTransitionOffsets()
        {
            _transitionOffset = _progress;
            _invertedTransitionOffset = (_progress - 1.0f) * -1;
        }

        protected override void EndUpdate()
        {
            base.EndUpdate();
            if (_progress != 1.0f) base.NeedsUpdate = true;
        }

        #endregion

        public override void Reset()
        {
            base.Reset();
            _count = 0.0f;
            _progress = 0.0f;
            _transitionOffset = 0.0f;
            _invertedTransitionOffset = 0.0f;
            base.BasicEffect.Alpha = 1.0f;
        }

    }

}
